﻿using System.Collections.Generic;
using System.Linq;
using Missbot.Common;
using Missbot.Core.Base;
using Missbot.Core.Cache;
using Missbot.Core.Common;
using Dolany.UtilityTool;
using Missbot.Core.CrossingRoute;
using Missbot.Model.MsgInfos;

namespace Missbot.AIModule.Gaming.TouhouCard
{
    /// <summary>
    /// 东方卡牌服务
    /// </summary>
    public class TouhouCardSvc : ISingleton, IDataMgr, IViewable
    {
        private List<InstanceFile> AllFiles { get; set; }

        public InstanceFile this[string cardName] => AllFiles.FirstOrDefault(file => file.Name == cardName);

        public void RefreshData()
        {
            var folderID = Configger<AIConfigBase>.AIConfig.TouhouCard?.FolderID;
            AllFiles = string.IsNullOrEmpty(folderID) ? new List<InstanceFile>() : Requestor.GetFolderFiles(folderID).ToList();
        }

        /// <summary>
        /// 展示指定的卡牌
        /// </summary>
        /// <param name="cardName"></param>
        /// <param name="session"></param>
        public void ShowCard(string cardName, InboxSession session)
        {
            session.SendBack(this[cardName].Name, this[cardName].ID);
        }

        /// <summary>
        /// 获取随机卡牌名
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public string RandomCard(string UserID)
        {
            return RapidCacher.GetCache($"TouhouCard:{UserID}", CommonUtil.UntilTommorow(), () => AllFiles.RandElement().Name);
        }

        public (bool canView, string viewCommand) ViewableCheck(string name)
        {
            return this[name] == null ? (false, null) : (true, "查看卡牌");
        }
    }
}